function AbsPopupImage() {
};

AbsPopupImage.gui = function(doc, unique) {
  return { panel: document.getElementById(unique + '_panel_div')
         };
};

function PopupImage(doc, container, width, height, user, date, options) {
  this._doc = document;
  this._container = container;
  this._width = (width) ? width : '192';
  this._height = (height) ? height: '154';
  this._user = user;
  this._date = date;
  this._options = options;
  this._unique = PopupImage.unique();
  
  this._createElements();
  this._gui = AbsPopupImage.gui(this._doc, this._unique);
}

PopupImage.prototype._createElements = function() {
  var table, tr, td, input;
  table = this._doc.createElement('table');
  table.style.width = this._width + 'px';
  table.style.height = this._height + 'px';
  table.cellPadding = 0;
  table.cellSpacing = 0;
  this._container.appendChild(table);
  
  tr = table.insertRow(-1);
  td = tr.insertCell(-1);
  td.style.width = (this._width) + 'px';
  td.style.height = (this._height) + 'px';
  td.style.textAlign = 'left';
  td.style.verticalAlign = 'top';
  td.id = this._unique + '_panel_div';
};

PopupImage.unique = function() {
  var unique = 'u' + parseInt(new Date().getTime() * Math.random());
  unique = unique.replace('.', '');
  unique.substring(0, 16);
  return unique;
};

function ModulePopupImage() {
  this._background = null;
  this._container = null;
}

ModulePopupImage.prototype._reset = function(pos) {
  this._close();
  
  var size = _xDocSize(this._doc);
  
  this._background = this._doc.createElement('div');
  this._background.style.width = size.w + 'px';
  this._background.style.height = size.h + 'px';
  this._background.style.position = 'absolute';
  this._background.style.top = '0px';
  this._background.style.left = '0px';
  this._background.style.display = 'block';
  this._background.style.background = 'transparent';
  this._doc.body.appendChild(this._background);
  
  this._container = this._doc.createElement('div');
  this._container.style.position = 'absolute';
  this._container.style.opacity = '1';
  this._container.style.background = '#1B1B1B';
  this._container.style.border = 'solid 2px #6F6F6F';
  this._container.style.top = ((this._position[1] - 60) > 0) ? (this._position[1] - 60) + 'px' : '0px';
  this._container.style.left = ((this._position[0] + 200) > window.screen.width) ? (window.screen.width - 200) + 'px' : this._position[0] + 'px';
  this._background.appendChild(this._container);

  var posX = this._position[0];
  var posY = ((this._position[1] - 100) > 0) ? (this._position[1] - 100) : 0;
  //window.scrollTo(posX, posY);
};

ModulePopupImage.prototype._open = function(container, options) {
  var table, tr, td, _self = this;
  
  this._position = (options && options.pos) ? options.pos : [((document.body.offsetWidth / 2) - 50), ((document.body.offsetHeight / 2) - 50)];
  this._doc = options.document;
  
  this._reset();
  
  this._gui = new PopupImage(options.document, this._container, options.width, options.height, null, null, options)._gui;
  
//  this._background.onclick = function() { _self._close(); };
  
  this._gui.panel.appendChild(container);
};

ModulePopupImage.prototype._close = function() {
  if (this._background) {
    this._doc.body.removeChild(this._background);
    this._background = null;
    this._container = null;
  }
};

function _xDocSize() {
  var b = document.body, e = document.documentElement;
  var esw = 0, eow = 0, bsw = 0, bow = 0, esh = 0, eoh = 0, bsh = 0, boh = 0;
  if (e) {
    esw = e.scrollWidth;
    eow = e.offsetWidth;
    esh = e.scrollHeight;
    eoh = e.offsetHeight;
  }
  if (b) {
    bsw = b.scrollWidth;
    bow = b.offsetWidth;
    bsh = b.scrollHeight;
    boh = b.offsetHeight;
  }
  return {
    w : Math.max(esw, eow, bsw, bow),
    h : Math.max(esh, eoh, bsh, boh)
  };
};



 	